package com.example.administrator.surfaceviewanimation;


import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.widget.Button;


public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback   {
    public static final String TAG = MainActivity.class.getSimpleName();
    MediaPlayer player;
    MySurfaceView surface;
    SurfaceHolder surfaceHolder;
    Button play,pause,stop;
    private  boolean  flag =false;
    private  int width = 1000;
    private  int height =1000;
    private int count =10;

    private int changeWith = 60;
    private int currentWidth = 60;
    private int changeCurrentWith;
    private int x= 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        play = (Button) findViewById(R.id.button1);
        pause = (Button) findViewById(R.id.button2);
        stop = (Button) findViewById(R.id.button3);
        surface = (MySurfaceView) findViewById(R.id.surface);
        surfaceHolder = surface.getHolder();//SurfaceHolder是SurfaceView的控制接口
        surfaceHolder.addCallback(this); //因为这个类实现了SurfaceHolder.Callback接口，所以回调参数直接this
//        surfaceHolder.setFixedSize(320, 220);//显示的分辨率,不设置为视频默认
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//Surface类型
        flag =true;

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.start();

            }
        });
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.pause();
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                player.stop();
            }
        });
        surface.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                if (flag){
                    ViewGroup.LayoutParams lp =  surface.getLayoutParams();
                    lp.width = getWindowManager().getDefaultDisplay().getWidth();
                    lp.height = getWindowManager().getDefaultDisplay().getHeight();
                    Log.e(TAG,"width=="+width);
                    Log.e(TAG,"height=="+height);
                    surface.setLayoutParams(lp);
                    flag = false;
                }else {
                    ViewGroup.LayoutParams lp =  surface.getLayoutParams();
                    lp.width = 240;
                    lp.height = 240;
                    surface.setLayoutParams(lp);
                    flag = true;
                }

//                setAnimRun(surface);
//            int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
//            int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
//            surface.measure(w, h);
//            int width =surface.getMeasuredWidth();
//            int height =surface.getMeasuredHeight();
//                currentWidth = surface.getWidth();
//                if (count==0){
//                    return;
//                }
//                changeWith = (width - currentWidth)/count;
//                Log.e(TAG,"changeWith="+changeWith);
//                while (count >0){
//                    handler.sendEmptyMessageDelayed(9, 300);
//                    Log.e(TAG,"send");
//                    count--;
//                }
            }
        });

//        surface.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
//
//            @Override
//            public void onGlobalLayout() {
//                //判断系统版本是否大于16
//                if (Build.VERSION.SDK_INT >= 16) {
//                    surface.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//                }
//                else {
//                    surface.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//                }
//
//                ViewGroup.LayoutParams lp =  surface.getLayoutParams();
//                    lp.width = surface.getWidth();
//                    lp.height =surface.getHeight();
//                    Log.e(TAG,"handlerhandlerhandlerhandler  lp.width="+ lp.width );
//                    surface.setLayoutParams(lp);
//            }
//        });
//    }
//        surface.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
//            @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//                //移除监听
////                surface.removeOnLayoutChangeListener(this);
//                ViewGroup.LayoutParams lp =  surface.getLayoutParams();
//                lp.width = surface.getWidth(); // 获取宽度
//                lp.height =  surface.getHeight(); // 获取高度
//                Log.e(TAG,"handlerhandlerhandlerhandler  lp.width="+ lp.width );
//                surface.setLayoutParams(lp);
//            }
//        });

//        ViewTreeObserver vto = surface.getViewTreeObserver();
//        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
//            public boolean onPreDraw() {
//                int height = surface.getMeasuredHeight();
//                int width = surface.getMeasuredWidth();
//
//                Log.e("surfaceview","width="+width+",height="+height);
//                return true;
//            }
//        });
    }
    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

        player.setDisplay(surfaceHolder);
    }
    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        Log.e(TAG,"width=="+surface.getWidth());
        //必须在surface创建后才能初始化MediaPlayer,否则不会显示图像
        player=new MediaPlayer();
        player.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
        player.setDisplay(surfaceHolder);
        String path = "http://dhlive.buddiestv.com/video-40-s-42-t-0-2-cn.m3u8?t=14327156465801";

        //设置显示视频显示在SurfaceView上
        try {
            player.setDataSource(path);
            player.prepare();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
    }
    @Override    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if(player.isPlaying()){
            player.stop();        }
        player.release();
        //Activity销毁时停止播放，释放资源。不做这个操作，即使退出还是能听到视频播放的声音
    }

    public void setAnimRun(View view){
        ObjectAnimator animator = ObjectAnimator.ofFloat(view,"txz",1.0f,3.0f).setDuration(2000);
        animator.start();


        WindowManager manager = (WindowManager) this
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        int totalWidth = display.getWidth();
        int totalHeight = display.getHeight();
        float ratioWidth = (float) totalWidth / (float) view.getWidth();
        float ratioHeight = (float) totalHeight / (float)  view.getHeight();
        ObjectAnimator bigAnimatorScaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f, ratioWidth);
        ObjectAnimator bigAnimatorScaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f, ratioHeight);
        float curTranslationX = view.getTranslationX();
        ObjectAnimator bigAnimatorTranslationX = ObjectAnimator.ofFloat(view, "translationX", curTranslationX, totalWidth / (view.getWidth() / 2));
        float curTranslationY = view.getTranslationY();
        ObjectAnimator bigAnimatorTranslationY = ObjectAnimator.ofFloat(view, "translationY", curTranslationY, totalHeight / (view.getHeight() / 2));
        AnimatorSet bigAnimSet = new AnimatorSet();
        bigAnimSet.play(bigAnimatorScaleX).with(bigAnimatorScaleY).with(bigAnimatorTranslationX).with(bigAnimatorTranslationY);
        bigAnimSet.setDuration(3000);
        bigAnimSet.addListener(new viewAnimationListener());
        bigAnimSet.start();

    }
    private class viewAnimationListener implements Animator.AnimatorListener {


        @Override
        public void onAnimationStart(Animator animator) {

        }

        @Override
        public void onAnimationEnd(Animator animator) {
//            int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
//            int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
//            surface.measure(w, h);
//            int width =surface.getMeasuredWidth();
//            int height =surface.getMeasuredHeight();
//            ViewGroup.LayoutParams lp =  surface.getLayoutParams();
//            lp.width = width;
//            lp.height = height;
//            Log.e(TAG,"width=="+width);
//            Log.e(TAG,"height=="+height);
//            surface.setLayoutParams(lp);

        }

        @Override
        public void onAnimationCancel(Animator animator) {

        }

        @Override
        public void onAnimationRepeat(Animator animator) {


//            surfaceHolder.setFixedSize();
        }
    }

//    private  Handler handler = new Handler(){
//        @Override
//        public void handleMessage(Message msg) {
//            if (msg.what == 9) {
//
//                if (x<=10){
//                    ViewGroup.LayoutParams lp =  surface.getLayoutParams();
//                    lp.width = currentWidth+changeWith*x;
//                    lp.height =currentWidth+changeWith*x;
//                    Log.e(TAG,"handlerhandlerhandlerhandler  lp.width="+ lp.width );
//                    surface.setLayoutParams(lp);
//                }
//
//                x++;
//
//            }
//        }
//    };
}
